package com.ruoyi.legal.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.legal.domain.vo.AjtzBatchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.legal.mapper.AjtzMapper;
import com.ruoyi.legal.domain.Ajtz;
import com.ruoyi.legal.service.IAjtzService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 案件台账Service业务层处理
 * 
 * @author xwj
 * @date 2025-04-17
 */
@Service
public class AjtzServiceImpl extends ServiceImpl<AjtzMapper, Ajtz> implements IAjtzService {
    @Autowired
    private AjtzMapper ajtzMapper;

    /**
     * 查询案件台账
     * 
     * @param id 案件台账主键
     * @return 案件台账
     */
    @Override
    public Ajtz selectAjtzById(Long id) {
        return ajtzMapper.selectAjtzById(id);
    }

    /**
     * 查询案件台账列表
     * 
     * @param ajtz 案件台账
     * @return 案件台账
     */
    @Override
    public List<Ajtz> selectAjtzList(Ajtz ajtz) {
        return ajtzMapper.selectAjtzList(ajtz);
    }

    /**
     * 新增案件台账
     * 
     * @param ajtz 案件台账
     * @return 结果
     */
    @Override
    public Ajtz insertAjtz(Ajtz ajtz) {

      //  ajtzMapper.insertAjtz(ajtz);
    save(ajtz);
    return ajtz;
    }

    /**
     * 修改案件台账
     * 
     * @param ajtz 案件台账
     * @return 结果
     */
    @Override
    public Ajtz updateAjtz(Ajtz ajtz) {

      //  ajtzMapper.updateAjtz(ajtz);
        updateById(ajtz);
        return ajtz;
    }

    /**
     * 批量删除案件台账
     * 
     * @param ids 需要删除的案件台账主键
     * @return 结果
     */
    @Override
    public int deleteAjtzByIds(Long[] ids) {
        return ajtzMapper.deleteAjtzByIds(ids);
    }

    /**
    * 条件删除
    * @param ajtz 案件台账
    * @return
    */
    @Override
    public int delete(Ajtz ajtz) {
        QueryWrapper<Ajtz> qw = new QueryWrapper<>();
        QueryTool.condition(qw, ajtz);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param ajtz 案件台账
     * @return
     */
    @Override
    public Pages<Ajtz> queryPage(Ajtz ajtz) {
        QueryWrapper<Ajtz> qw = new QueryWrapper<>();
        QueryTool.condition(qw, ajtz);
        IPage<Ajtz> ipage = new Query<Ajtz>().getPage(ajtz);

        IPage<Ajtz> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    @Override
    @Transactional
    public void batchSaveUpdate(AjtzBatchVo ajtzBatchVo, SysUser user) {
        List<Ajtz> ajtzList = ajtzBatchVo.getList();
        List<Long> ids = ajtzBatchVo.getDeleteIds();

        Date now = new Date();
        String nowStr = DateUtil.formatDateTime(DateUtil.date());

        for(Ajtz ajtz: ajtzList) {
            if(ajtz.getId() == null) {
                ajtz.setStatus("0");
                ajtz.setCreateId(user.getUserId());
                ajtz.setCreateBy(user.getNickName());
                ajtz.setCreateTime(now);

                if(StringUtils.isEmpty(ajtz.getLasj())) {
                    ajtz.setLasj(nowStr);
                }
            } else {
                ajtz.setUpdateId(user.getUserId());
                ajtz.setUpdateTime(DateUtils.getNowDate());
                ajtz.setUpdateBy(user.getNickName());
            }
        }

        saveOrUpdateBatch(ajtzList);
        if(ids != null && ids.size() > 0) {
            deleteAjtzByIds(ids.toArray(new Long[0]));
        }

    }

    @Override
    public Double countFmks(Ajtz ajtz) {
        return baseMapper.countFmks(ajtz);
    }

    /**
     * 删除案件台账信息
     * 
     * @param id 案件台账主键
     * @return 结果
     */
    @Override
    public int deleteAjtzById(Long id) {
        return ajtzMapper.deleteAjtzById(id);
    }
}
